<template>
  <div class="reviews-container" v-if="goodsComments.length > 0">
    <a-row v-if="loading" :gutter="[16, 16]">
      <a-col :span="24" v-for="i in 3" :key="i">
        <a-skeleton active :paragraph="{ rows: 3 }" />
      </a-col>
    </a-row>

    <a-row v-else :gutter="[16, 16]">
      <a-col :span="24" v-for="(review, index) in displayedReviews" :key="index" class="review-item">
        <div class="review-content">
          <div class="review-left">
            <!-- Star rating -->
            <div class="star-rating">
              <a-image 
                v-for="(item, starIndex) in 5" 
                :key="starIndex" 
                style="width: 15px; height: 15px; margin-right: 2px;" 
                :src="SVG_ICONS.Xing" 
              />
            </div>
            <!-- Review date -->
            <div class="review-date">{{ review.create_time }}</div>
            <!-- Review text -->
            <div class="review-text">{{ review.content }}</div>
            <div v-if="review.images && review.images.length" class="review-images">
              <a-image 
                v-for="(image, imgIndex) in review.images" 
                :key="imgIndex" 
                :src="image" 
                class="review-image"
                @click="handleImageClick(image)"
              />
            </div>
          </div>
          <div class="review-right">
            <div class="reviewer-name">{{ review.user_name }}</div>
            <img :src="SVG_ICONS.feefo" alt="">
          </div>
        </div>
      </a-col>
    </a-row>
    <div class="reviews-more" v-if="hasMoreReviews">
      <div class="reviews-more-button" type="primary" @click="loadMore">
        View Next 5 Reviews 
        <a-spin v-if="loadingMore" />
      </div>
    </div>
  </div>
  <div v-else class="reviews-container" style="padding: 20px;text-align: center;border-bottom: 1px solid #e8e8e8;">
    <div class="reviews-title" >No product reviews have been submitted as of yet.</div>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed, PropType } from 'vue';
import SVG_ICONS from "@/assets/SVG_ICONS";

interface Review {
    create_time: string;
    content: string;
    user_name: string;
    images: string[];
}

const props = defineProps({
  goodsComments: {
    type: Array as PropType<Review[]>,
    required: true
  }
})

const loading = ref(true);
const currentPage = ref(1);
const reviewsPerPage = 5;
const loadingMore = ref(false);

const displayedReviews = computed(() => {
  return props.goodsComments.slice(0, currentPage.value * reviewsPerPage);
});

const hasMoreReviews = computed(() => {
  return displayedReviews.value.length < props.goodsComments.length;
});

const loadMore = async () => {
  loadingMore.value = true;
  currentPage.value += 1;
  await new Promise(resolve => setTimeout(resolve, 2000));
  loadingMore.value = false;
};

const handleImageClick = (image: string) => {
  console.log('Image clicked:', image);
};

setTimeout(() => {
  loading.value = false;
}, 1000);
</script>

<style lang="scss" scoped>
.reviews-container {
  width: 70%;
  margin: 0 auto;
  padding: 20px;
  background-color: #f9f9f9;
    color: #000;
    border: none;
    border-radius: .3125rem;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .5);
    line-height: normal;
    margin-bottom:20px;
}

.review-item {
  border-bottom: 1px solid #e8e8e8;
  padding: 16px 0;
  &:last-child {
    border-bottom: none;
  }
}

.review-content {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.review-left {
  flex: 1;
  margin-right: 20px;
  border-right: 1px solid #e8e8e8;
  padding-right: 20px;

  .star-rating {
    margin-bottom: 8px;
  }

  .review-date {
    color: #999;
    font-size: 14px;
    margin-bottom: 8px;
  }

  .review-text {
    color: #000;
    font-size: 16px;
    line-height: 1.5;
    margin-bottom: 12px;
  }

  .review-images {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-top: 12px;

    .review-image {
      width: 80px;
    //   height: 80px;
      object-fit: cover;
      border-radius: 4px;
      cursor: pointer;
      transition: transform 0.2s ease;

      &:hover {
        transform: scale(1.05);
      }
    }
  }
}

.review-right {
    width: 100px;
  .reviewer-name {
    font-size: 14px;
    font-weight: bold;
  }
}
.reviews-more {
  // 按钮局中
  display: flex;
  justify-content: center;
  align-items: center;

  margin-top: 20px;
  .reviews-more-button {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #fff;
    color: #000;
    border: 1px solid #000;
    padding: 8px 20px;
      border-radius: 30px;
      font-weight: bold;
    font-size: 14px;
  }
}
</style>